package com.gccloud.dataroom.core.module.message.controller;

import com.gccloud.common.permission.ApiPermission;
import com.gccloud.common.vo.MixinsResp;
import com.gccloud.dataroom.core.config.DataRoomConfig;
import com.gccloud.dataroom.core.constant.DataRoomConst;
import com.gccloud.dataroom.core.module.manage.service.IDataRoomPagePreviewService;
import com.gccloud.dataroom.core.module.manage.service.IDataRoomPageService;
import com.gccloud.dataroom.core.module.message.dto.ChangeUrlBody;
import com.gccloud.dataroom.core.module.message.dto.CmdMessageDTO;
import com.gccloud.dataroom.core.module.message.dto.DataRoomMessageDTO;
import com.gccloud.dataroom.core.module.message.vo.DataRoomSendVO;
import com.gccloud.dataroom.core.permission.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author hongyang
 * @version 1.0
 * @date 2023/3/13 10:55
 */
@Slf4j
@RestController
@RequestMapping("/bigScreen/message")
@Api(tags = "大屏页以及大屏组件设计")
public class DataRoomMessageController {

    @Resource
    private IDataRoomPageService bigScreenPageService;
    @Resource
    private DataRoomConfig bigScreenConfig;
    @Resource
    private IDataRoomPagePreviewService previewService;

    @Resource
    private RestTemplate restTemplate;

    @ApiPermission(permissions = {Permission.Message.SEND})
    @PostMapping("/publish")
    @ApiOperation(value = "大屏页/发布页面", position = 10, produces = MediaType.APPLICATION_JSON_VALUE)
    public MixinsResp<DataRoomSendVO> send(@RequestBody DataRoomMessageDTO dataRoomMessageDTO) {
        //        if (code.startsWith(IDataRoomPagePreviewService.PREVIEW_KEY)) {
//            PagePreviewEntity preview = previewService.getByCode(code);
//            MixinsResp<DataRoomPageDTO> r = new MixinsResp<DataRoomPageDTO>().setData((DataRoomPageDTO) preview.getConfig());
//            r.setCode(DataRoomConst.Response.Code.SUCCESS);
//            return r;
//        }
//        PageEntity bigScreen = bigScreenPageService.getByCode(code);
//        DataRoomPageDTO bigScreenPageDTO = (DataRoomPageDTO) bigScreen.getConfig();
//        BeanConvertUtils.convert(bigScreen, bigScreenPageDTO);

        System.out.println(dataRoomMessageDTO);

        CmdMessageDTO cmdMessageDTO =  new CmdMessageDTO();

        ChangeUrlBody changeUrlBody = new ChangeUrlBody();
        changeUrlBody.setUrl(dataRoomMessageDTO.getUrl());

        cmdMessageDTO.setCmd("1");
        cmdMessageDTO.setType("1");
        cmdMessageDTO.setBody(changeUrlBody);

        String ip = dataRoomMessageDTO.getIp();
        String port = dataRoomMessageDTO.getPort();

        String url = "http://"+ip+":" + port+ "/message";
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(
                url, cmdMessageDTO, String.class);

//        return responseEntity.getBody()
        System.out.println(responseEntity);


        MixinsResp<DataRoomSendVO> resp = new MixinsResp<DataRoomSendVO>().setData(new DataRoomSendVO());
        resp.setCode(DataRoomConst.Response.Code.SUCCESS);
        return resp;
    }


}
